from django.shortcuts import render
from django.views.generic.base import View
from django.http.response import JsonResponse
from django.http.request import HttpRequest

import json
# Create your views here.

class JMJsonBaseView(View):
    def dispatch(self, request:HttpRequest, *args, **kwargs):

        data = None
        if request.body:
            try:
                data = json.loads(request.body, encoding='utf-8')
            except Exception as e:
                pass

        result = super(JMJsonBaseView, self).dispatch(request, data, *args, **kwargs)
        if not isinstance(result, dict):
            '''
                这里的错误我们应该扼杀在开发阶段。
            '''
            raise TypeError('Your method in views should return [dict] object')

        json_dumps_params = {'ensure_ascii': False}

        return JsonResponse(result, json_dumps_params=json_dumps_params)

    def get(self, request, data):
        pass

    def post(self, request, data):
        pass